<?php
/*********************************************************************************
** The contents of this file are subject to the vtiger CRM Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
*
 ********************************************************************************/


function getNoofFaqsPerCategory($category_name)
{
	$faq_array = $_SESSION['faq_array'];
	$count = 0;
	for($i=0;$i<count($faq_array);$i++)
	{
		if($category_name == $faq_array[$i]['category'])
			$count++;
	}
	return $count;
}
function getNoofFaqsPerProduct($productid)
{
	$faq_array = $_SESSION['faq_array'];
	$count = 0;
	for($i=0;$i<count($faq_array);$i++)
	{
		if($productid == $faq_array[$i]['product_id'])
			$count++;
	}
	return $count;
}

function getLatestlyCreatedFaqList() {
	$list = '';
	$product_array = $_SESSION['product_array'];
	$faq_array = $_SESSION['faq_array'];
	$list = '<div id="faqArea">' . "\n";
	$list .= '<span class="addimage">' . getTranslatedString('LBL_RECENTLY_CREATED') . '</span>' . "\n";
	
	for($i=0;$i<count($faq_array);$i++) {
		$record_exist = true;
		$list .= '<div class="faqRec">
		   <img src="images/faq.gif" class="faqRec" alt="Faq" />
		   <a class="faqQues" href="index.php?module=Faq&amp;action=index&amp;fun=faq_detail&amp;faqid=' . $faq_array[$i]['id'] . '">' . $faq_array[$i]['question'] . '</a>
			<div class="faqAnsw">' . $faq_array[$i]['answer'] . '</div>
			</div>' . "\n";
	}
	if(!$record_exist) {
		$list .= '<div class="faqRec">' . getTranslatedString('LBL_NO_FAQ') . '</div>' . "\n";
   }
	$list .= '</div><!-- End faqArea -->' . "\n";
	return $list; 
}

function ListFaqsPerCategory($category_index) {
	$list = '';
	$category_array = $_SESSION['category_array'];
	$faq_array = $_SESSION['faq_array'];
	$category = $category_array[$category_index];
	$list = '<div id="faqArea">' . "\n";
	$list .= '<span class="addimage">' . getTranslatedString('LNK_CATEGORY') . ': ' . $category . '</span>' . "\n";

	for($i=0;$i<count($faq_array);$i++)
	{
		if($category == $faq_array[$i]['category'])
		{
			$flag = true;
			$list .= '<div class="faqRec">
            <img src="images/faq.gif" class="faqRec" alt="Faq" />
				<a class="faqQues" href="index.php?module=Faq&amp;action=index&amp;fun=faq_detail&amp;faqid=' . $faq_array[$i]['id'] . '">' . $faq_array[$i]['question'] . '</a>
				<div class="faqAnsw">' . $faq_array[$i]['answer'] . '</div>
				</div>' . "\n";
		}
	}
	if(!$flag) {
		$list .= '<div class="faqRec">' . getTranslatedString('LBL_NO_FAQ_IN_THIS_CATEGORY') . '</div>' . "\n";
	}
	$list .= '</div><!-- End faqArea -->' . "\n";
	return $list; 
}

function ListFaqsPerProduct($productid) {
	$list = '';
	$product_array = $_SESSION['product_array'];
	$faq_array = $_SESSION['faq_array'];
	$list = '<div id="faqArea">' . "\n";
	$list .= '<span class="addimage">' . getTranslatedString('LBL_PRODUCT') . ': ' .getProductname($productid) . '</span>' . "\n";
	
	for($i=0;$i<count($faq_array);$i++) {
		if($productid == $faq_array[$i]['product_id']) {
			$flag = true;
			$list .= '<div class="faqRec">
            <img src="images/faq.gif" class="faqRec" alt="faq" />
				<a class="faqQues" href="index.php?module=Faq&amp;action=index&amp;fun=faq_detail&amp;faqid=' . $faq_array[$i]['id'] . '">' . $faq_array[$i]['question'] . '</a>
				<div class="faqAnsw">' . $faq_array[$i]['answer'] . '</div>
				</div>' . "\n";
		}
	}
	if(!$flag) {
		$list .= '<div class="faqRec">' . getTranslatedString('LBL_NO_FAQ_IN_THIS_PRODUCT') . '</div>' . "\n";
	}
	$list .= '</div><!-- End faqArea -->' . "\n";
	return $list; 
}

function getArticleIdTime($faqid,$product_id,$faqcategory,$faqcreatedtime,$faqmodifiedtime)
{
	$list .='<div id="faqDetail" onMouseOver="fnShowDiv(\'faqDetail\')" onMouseOut="fnHideDiv(\'faqDetail\')">
		 <table class="fagView">
		   <tr>
			<td align="right"><b>'.getTranslatedString('LBL_FAQ_ID').': </b></td><td align="left"><b>'.$faqid.'</b></td>
		   </tr>
		   <tr>
			<td align="right">'.getTranslatedString('LBL_PRODUCT').': </td><td align="left">'.getProductName($product_id).'</td>
		   </tr>
		   <tr>
			<td align="right">'.getTranslatedString('LBL_CATEGORY').': </td><td align="left">'.$faqcategory.'</td>
		   </tr>
		   <tr>
			<td align="right">'.getTranslatedString('LBL_CREATED_DATE').': </td><td align="left">'.substr($faqcreatedtime,0,10).'</td>
		   </tr>
		   <tr>
			<td align="right" nowrap>'.getTranslatedString('LBL_MODIFIED_DATE').': </td><td align="left">'.substr($faqmodifiedtime,0,10).'</td>
		   </tr>
		</table>
		</div>';

	return $list;
}
function getPageOption()
{
	$list .= '
			<table width="100%" border="0" cellspacing="3" cellpadding="3">
		   	   <tr>
				<td width="18" align="center"><img src="images/print.gif" valign="absmiddle"></td><td><a href="javascript:printPage()">'.getTranslatedString('LBL_PRINT_THIS_PAGE').'</a></td>
				<td width="18" align="center"><img src="images/email.gif" valign="absmiddle"></td><td><a href="javascript:sendAsEmail();">'.getTranslatedString('LBL_EMAIL_THIS_PAGE').'</a></td>
				<td width="18" align="center"><img src="images/favorite.gif" valign="absmiddle"></td><td><a href="javascript:addToFavorite();">'.getTranslatedString('LBL_ADD_TO_FAVORITES').'</a></td>
			   </tr>
			</table>
		';
	$list .= '<script type="JavaScript">
				function printPage() {
					window.print()
				}
				function sendAsEmail() {
					var emailBody=escape("'.getTranslatedString('LBL_ARTICLE_INTERESTED').'"+String.fromCharCode(13)+String.fromCharCode(13)+"URL: "+document.location.href)
					document.location.href = "mailto:?body="+emailBody;
				}
				function addToFavorite() {
					if (document.all) {
						window.external.addFavorite(document.location.href,document.title);
					} else {
						alert("'.getTranslatedString('LBL_PRESS_CNTR_D').'")
					}
				}
			</script>';
	
	return $list;
}
function getProductName($productid)
{
	$product_array = $_SESSION['product_array'];
	$productname = '';
	for($i=0;$i<count($product_array);$i++)
	{
		if($productid == $product_array[$i]['productid'])
			$productname = $product_array[$i]['productname'];
	}
	return $productname;
}
function getSearchCombo()
{
	$category_array = $_SESSION['category_array'];
	$product_array = $_SESSION['product_array'];
	$comboarray = '<select name="search_category">' . "\n";
	$comboarray .= '<option value="all:All">All</option>' . "\n";
	$comboarray .= '<optgroup label="Categories">' . "\n";
	for($i=0;$i<count($category_array);$i++)
	{
		$selected = '';
		$search_category = explode(":",$_REQUEST['search_category']);
		if($category_array[$i] == $search_category[1])
			$selected = 'selected="selected"';
		$comboarray .= '<option value="category:' . $category_array[$i] . '"' . $selected . '>' . $category_array[$i] . '</option>' . "\n";
	}
	$comboarray .= '</optgroup>' . "\n";
	$comboarray .= '<optgroup label="Products">' . "\n";
        for($i=0;$i<count($product_array);$i++)
        {
                $selected = '';
		$search_category = explode(":",$_REQUEST['search_category']);
                if($product_array[$i]['productname'] == $search_category[1])
                        $selected = 'selected="selected"';
                $comboarray .= '<option value="products:' . $product_array[$i]['productname'] . '"' . $selected . '>' . $product_array[$i]['productname'] . '</option>' . "\n";
        }
        $comboarray .= '</optgroup>' . "\n";
	$comboarray .= '</select>' . "\n";
	return $comboarray;
}
function getSearchResult($search_text,$search_value,$search_by)
{
	$faq_array = $_SESSION['faq_array'];
	
	$list = '<div id="faqArea">' . "\n";
	$list .= '<span class="addimage">' . getTranslatedString('LBL_SEARCH_RESULT') . '</span>' . "\n";

	if($search_value == 'All') {
      for($i=0;$i<count($faq_array);$i++) {
			if($search_text != '') {
            $flag = @stristr($faq_array[$i]['question'],$search_text);
			} else {
				$flag = true;
         }
         if($flag) {
				$record_exist = true;
            $list .= '<div class="faqRec">
					<img src="images/faq.gif" class="faqRec" alt="Faq" />
			      <a class="faqQues" href="index.php?module=Faq&amp;action=index&amp;fun=faq_detail&amp;faqid=' . $faq_array[$i]['id'] . '">' . $faq_array[$i]['question'] . '</a>
					<div class="faqAnsw">' . $faq_array[$i]['answer'] . '</div>
					<div class="kbFAQInfo">' . getTranslatedString('LBL_CATEGORY') . ': ' . $faq_array[$i]['category'] . '</div>
					</div>' . "\n";
         }
      }
		if(!$record_exist) {
         $list .= '<div class="faqRec">' . getTranslatedString('LBL_NO_FAQ_IN_THIS_SEARCH_CRITERIA') . '</div>' . "\n";
      }
   } elseif($search_by == 'category') {
      for($i=0;$i<count($faq_array);$i++) {
		   if($search_text != '') {
            $flag = @stristr($faq_array[$i]['question'],$search_text);
         } else {
   			$flag = true;
   		}
         if($flag && $faq_array[$i]['category'] == $search_value) {
			   $record_exist = true;
            $list .= '<div class="faqRec">
               <img src="images/faq.gif" class="faqRec" alt="Faq" />
					<a class="faqQues" href="index.php?module=Faq&amp;action=index&amp;fun=faq_detail&amp;faqid=' . $faq_array[$i]['id'] . '">' . $faq_array[$i]['question'] . '</a>
					<div class="faqAnsw">' . $faq_array[$i]['answer'] . '</div>
					</div>' . "\n";
         }
      }
		if(!$record_exist) {
			$list .= '<div class="faqRec">' . getTranslatedString('LBL_NO_FAQ_IN_THIS_SEARCH_CRITERIA') . '</div>' . "\n";
	   }
   }
	elseif($search_by == 'products') {
		$product_array = $_SESSION['product_array'];
		$faq_array = $_SESSION['faq_array'];
		for($i=0;$i<count($product_array);$i++) {
			if($product_array[$i]['productname'] == $search_value) {
				for($j=0;$j<count($faq_array);$j++)	{
					if($search_text != '') {
                  $flag = @stristr($faq_array[$j]['question'],$search_text);
               } else {
              		$flag = true;
              	}
		        	if($flag && ($product_array[$i]['productid'] == $faq_array[$j]['product_id'])) {
           			$record_exist = true;
                  $list .= '<div class="faqRec">
                     <img src="images/faq.gif" class="faqRec" />
							<a class="faqQues" href="index.php?module=Faq&amp;action=index&amp;fun=faq_detail&amp;faqid=' . $faq_array[$j]['id'] . '">' . $faq_array[$j]['question'] . '</a>
							<div class="faqAnsw">'.$faq_array[$j]['answer'].'</div>
							</div>' . "\n";
                }
            }
			}
		}
		if(!$record_exist) {
         $list .= '<div class="faqRec">' . getTranslatedString('LBL_NO_FAQ_IN_THIS_SEARCH_CRITERIA') . '</div>' . "\n";
      }
	}

	$list .= '</div><!-- End faqArea -->';
	return $list;
}

function text_length($str){
	$length = strlen($str);
	if($length > 25){
		$str = substr($str,0,25)."..";
		return $str;
	}
	return $str;
}
?>
